<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\controller\jia;

use app\api\controller\Controller;
use app\api\model\jia\Fapiao as FapiaoModel;
use app\api\model\User as UserModel;
use app\api\service\User as UserService;
use app\common\library\junziqian\Driver as DriverModel;
use cores\exception\BaseException;
use think\facade\Db;
use think\response\Json;

/**
 * 发票管理
 * Class Address
 * @package app\api\controller
 */
class Fapiao extends Controller
{

    public function list(): Json
    {
        // 获取收货地址列表
        $model = new FapiaoModel;
        $list  = $model->getList();
        return $this->renderSuccess(compact('list'));
    }
    public function getFaceResult(int $orderId = 11591): Json
    {
        $detail = FapiaoModel::detail(['order_id' => $orderId]);

        if ($detail->getFaceResult($this->postForm())) {
            return $this->renderSuccess([], '识别成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');

    }

    /**
     * 收货地址详情
     * @param int $orderId 地址ID
     * @return Json
     * @throws BaseException
     */
    public function getFapiao(int $orderId): Json
    {
        $detail = FapiaoModel::detail(['order_id' => $orderId]);
        if ($detail) {
            $data['real_name'] = $detail['real_name'];
            $data['mobile']    = $detail['mobile'];
            $data['sex']       = $detail['sex'];
            $data['user_id']   = $detail['user_id'];
            $data['idcard']    = $detail['idcard'];
            $data['age']       = $detail['age'];
            $contract          = $detail->add($orderId, $data);
        } else {
            $model = new FapiaoModel;

            $data        = Db::name('order')->where('order_id', $orderId)->field('user_id,real_name,mobile')->find();
            $data['sex'] = '男';

            $contract = $model->add($orderId, $data);
        }
       // $contract['path'] = base_url() . $contract['path'];

        return $this->renderSuccess(compact('contract'));
    }

    /**
     * 添加收货地址
     * @return Json
     * @throws BaseException
     */
    public function add(): Json
    {
        $model = new FapiaoModel;
        if ($model->add($this->postForm())) {
            return $this->renderSuccess([], '开票申请成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    public function get_idcard_ocr($url, $name): Json
    {
        $model = new DriverModel;

        $response = $model->getIdcardOcr($url, $name);

        return $this->renderSuccess(['formData' => $response['data']], '成功');
    }

    public function detail(int $orderId): Json
    {
        $userId = UserService::getCurrentLoginUserId();
        $detail = FapiaoModel::detail(['order_id' => $orderId], ['order' => ['build', 'manage', 'goods'], 'idcardUrl', 'idcardUrl2']);

        //企业认证

        // $model = new DriverModel;

        //  $response = $model->organizationCreate();

        if ($detail['face_mobile'] == '') {

            $model = new UserModel;
            $user  = $model::detail($userId, ['idcardUrl', 'idcardUrl2']);

            $detail['face_mobile'] = $user['face_mobile'];
            $detail['face_idcard'] = $user['face_idcard'];
            $detail['face_name']   = $user['face_name'];
            $detail['idcard1']     = $user['idcard1'];
            $detail['idcard2']     = $user['idcard2'];
            $detail['idcard1_url'] = $user['idcard1_url'];
            $detail['idcard2_url'] = $user['idcard2_url'];
        }
        //服务协议
        $detail['xieyi_url'] = base_url() . 'pdf/' . $detail['order']['goods'][0]['contract_type'] . '.pdf';

        if ($detail['contract_no'] != '') {

            //获取合同链接
            $response = $detail->getLinkFile();
        }

        return $this->renderSuccess(['detail' => $detail], '成功');
    }

    /**
     * 编辑-用户
     * @param int $orderId 地址ID
     * @return Json
     * @throws BaseException
     */
    public function editUser(int $orderId): Json
    {
        $model = FapiaoModel::detail(['order_id' => $orderId]);

        $info = $model->editUser($this->postForm());
        return $this->renderSuccess(compact('info'));
    }
    //更新人脸识别结果
    public function updateFace(int $orderId): Json
    {
        $model = FapiaoModel::detail(['order_id' => $orderId], ['order.build']);
        if ($model->updateFace($this->postForm())) {
            return $this->renderSuccess([], '更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }
    /**
     * 编辑
     * @param int $orderId 地址ID
     * @return Json
     * @throws BaseException
     */
    public function edit(int $orderId): Json
    {
        $model = FapiaoModel::detail(['order_id' => $orderId]);
        if ($model->edit($this->postForm())) {
            return $this->renderSuccess([], '更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 设为默认地址
     * @param int $orderId 地址ID
     * @return Json
     * @throws BaseException
     */
    public function setDefault(int $orderId): Json
    {
        $model = FapiaoModel::detail($orderId);
        if ($model->setDefault((int) $model['address_id'])) {
            return $this->renderSuccess([], '设置成功');
        }
        return $this->renderError($model->getError() ?: '设置失败');
    }

    /**
     * 删除收货地址
     * @param int $orderId 地址ID
     * @return Json
     * @throws BaseException
     */
    public function remove(int $orderId): Json
    {
        $model = FapiaoModel::detail($orderId);
        if ($model->remove()) {
            return $this->renderSuccess([], '删除成功');
        }
        return $this->renderError($model->getError() ?: '删除失败');
    }

}
